package main

import (
	"fmt"
	"sync"
	"time"
)

var group sync.WaitGroup

func write(ch chan<- int) {
	for i := 0; i < 5; i++ {
		ch <- i
		time.Sleep(time.Second)
	}
	close(ch)
	group.Done()
}
func read(ch <-chan int) {
	for val := range ch {
		fmt.Println("开始读取")
		fmt.Println(val)
		time.Sleep(time.Second * 2)
	}
	group.Done()
}

func main() {
	var ch = make(chan int, 20)
	group.Add(2)
	go write(ch)
	go read(ch)
	group.Wait()
}
